﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace Tos2Google
{
    [Serializable()]
    public class AutoLogin
    {
        public static AutoLogin StaticThis;

        public AutoLogin() { } // no-params constructor for XML deserializer
        public AutoLogin(bool justToHaveDifferentConstructor)
        {
            StaticThis = this;
            bool loadingConfXmlWasSuccess = Load();
            if (!loadingConfXmlWasSuccess)
                StaticThis.DefaultsForFailedLoad();
            AutoLoginForm autoLoginForm = new AutoLoginForm(loadingConfXmlWasSuccess);
            autoLoginForm.ShowDialog();
        }

        private void DefaultsForFailedLoad()
        {
        }

        public static void Edit()
        {
            if (AutoLoginDataForm.EditAutoLoginData(StaticThis))
                Save();
        }

        private static string FileName()
        {
            string rootFolder = Path.GetDirectoryName(Application.ExecutablePath);
            return Path.Combine(rootFolder, "AutoLoginData.xml");
        }

        public static bool Load()
        {
            string fileName = FileName();
            if (!File.Exists(fileName))
                return false;
            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(AutoLogin));
                using (TextReader textReader = new StreamReader(fileName))
                {
                    try
                    {
                        AutoLogin autoLogin = (AutoLogin)deserializer.Deserialize(textReader);
                        StaticThis.GetPropsFrom(autoLogin);
                        return true;
                    }
                    finally
                    {
                        textReader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("AutoLogin Load error: " + ex.Message);
                return false;
            }
        }

        public static void Save()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(AutoLogin));
                using (TextWriter textWriter = new StreamWriter(FileName()))
                {
                    try
                    {
                        serializer.Serialize(textWriter, StaticThis);
                    }
                    finally
                    {
                        textWriter.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("AutoLogin Save error: " + ex.Message);
            }
        }

        #region data fields (to/from XML file)

        public void GetPropsFrom(AutoLogin source) // have them all here, please
        {
            GUsername = source.GUsername;
            GPassword = source.GPassword;
            ProcessMode = source.ProcessMode;
            SheetName = source.SheetName;
            WorkSheetIndex = source.WorkSheetIndex;
            TD_Username = source.TD_Username;
            TD_Password = source.TD_Password;
            TDAC_LogLevel = source.TDAC_LogLevel;
        }

        public enum PreferredLoginMethodEnum { AutoLogin, DelayLogin, AllManual }
        public enum ProcessModeEnum { UploadToGoogle, DecisionEngine }

        [CategoryAttribute("Google login"), DisplayNameAttribute("Username (john@gmail.com)")]
        public string GUsername { get; set; }
        [CategoryAttribute("Google login"), DisplayNameAttribute("Password"), PasswordPropertyTextAttribute(true)]
        public string GPassword { get; set; }

        [CategoryAttribute("General"), DisplayNameAttribute("Process mode")]
        public ProcessModeEnum ProcessMode { get; set; }

        [CategoryAttribute("Spreadsheet"), DisplayNameAttribute("Sheet name")]
        public string SheetName { get; set; }
        [CategoryAttribute("Spreadsheet"), DisplayNameAttribute("Worksheet index (0, 1, ...)")]
        public int WorkSheetIndex { get; set; }

        [CategoryAttribute("TD Ameritrade"), DisplayNameAttribute("Username")]
        public string TD_Username { get; set; }
        [CategoryAttribute("TD Ameritrade"), DisplayNameAttribute("Password"), PasswordPropertyTextAttribute(true)]
        public string TD_Password { get; set; }
        [CategoryAttribute("TD Ameritrade"), DisplayNameAttribute("TDAC LogLevel"), DescriptionAttribute("0 means no log, otherwise the bigger the more detailed")]
        public int TDAC_LogLevel { get; set; }

        #endregion
    }
}
